﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.Text.Json;
using WeskyQuantDemo.Models;
using WeskyQuantDemo.Services;

namespace WeskyQuantDemo.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MyQuantController : ControllerBase
    {
        private readonly IQuantDataService _quantDataService;
        public MyQuantController(IQuantDataService quantDataService)
        {
            _quantDataService = quantDataService;
        }

        [HttpGet]
        public async Task<ActionResult<IEnumerable<StockBaseInfo>>> GetStockBaseInfos()
        {
            var apiUrl = "http://127.0.0.1:8080/api/public/stock_info_a_code_name";
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            var stockBaseInfos = await _quantDataService.GetStockInfosAsync<IEnumerable<StockBaseInfo>>(apiUrl);
            stopwatch.Stop();
            var elapsed = stopwatch.ElapsedMilliseconds;
            return Ok(stockBaseInfos);
        }

        [HttpPost]
        public async Task<ActionResult<string>> GetHistory()
        {
            var apiUrl = "http://127.0.0.1:8080/api/public/stock_zh_a_hist";
            var formData = new Dictionary<string, string>
            {
                 { "symbol", "300532" },
                { "period", "daily" },
                { "start_date", "20240101" },
                { "end_date", "20240623" },
                { "adjust", "qfq" }
            };
            // adjust 复权说明，qfq前复权，hfq后复权  空字符串 不复权
            var result = await _quantDataService.GetWithQueryParamsAsync(apiUrl, formData);
            IEnumerable<StockHistoryInfo> stockHistoryInfos = JsonSerializer.Deserialize<IEnumerable<StockHistoryInfo>>(result);

            return Ok(stockHistoryInfos);
        }
    }
}
